package com.example.activityproject;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("TAG","MainActivity 的onCreate已经执行~");
        Button to_oneact_bt = findViewById(R.id.to_oneact_bt);
        Button to_red_act_bt = findViewById(R.id.to_red_act_bt);
        Button to_blue_act_bt = findViewById(R.id.to_blue_act_bt);
        EditText input_et = findViewById(R.id.input_et);

        //点击监听 方法一(匿名内部类方式实现)
        to_oneact_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,OneActivity.class);
                //从MainActivity界面传值到OneActivity界面
                intent.putExtra("hello_key","我是 MainAct");
                MainActivity.this.startActivity(intent);

            }
        });

        to_red_act_bt.setOnClickListener((View view)->{
            Intent intent = new Intent(MainActivity.this,RedActivity.class);
            //使用Bundle对象包装数据传值
            Bundle bundle = new Bundle();
            bundle.putString("name","小明");
            bundle.putString("age","18");
            intent.putExtras(bundle);
            startActivity(intent);


        });

        //点击监听，方法二（主类实现OnClickListener）
        //设置监听
        to_blue_act_bt.setOnClickListener(this);

        Button get_red_setting_act_bt = findViewById(R.id.get_red_setting_act_bt);
        get_red_setting_act_bt.setOnClickListener(this);
        TextView test_tv = findViewById(R.id.test_tv);
        TextWatcher watcher = new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence text, int start, int count, int after) {
                //text  输入框中改变前的字符串信息
                //start 输入框中改变前的字符串的起始位置
                //count 输入框中改变前后的字符串改变数量一般为0
                //after 输入框中改变后的字符串与起始位置的偏移量


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                //text  输入框中改变后的字符串信息
                //start 输入框中改变后的字符串的起始位置
                //before 输入框中改变前的字符串的位置 默认为0
                //count 输入框中改变后的一共输入字符串的数量

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        };
        input_et.addTextChangedListener(watcher);

    }
    //重写点击事件的方法
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.to_blue_act_bt){//当点击view 的id为to_blue_act_bt时，证明是点击了to_blue_act_bt按钮
            //创建intent意图对象，确定跳转的界面
            Intent intent = new Intent(MainActivity.this, BlueAct.class);
            //创一个User类的对象，内容 name： 李白  age: 18
            User user = new User();
            user.setName("李白");
            user.setAge("18");
            intent.putExtra("user_key",user);
            startActivity(intent);
        }else if(v.getId()==R.id.get_red_setting_act_bt){//点击get_red_setting_act_bt按钮时
            //从MainActivity跳到RedActivity
            Intent intent = new Intent(MainActivity.this,RedActivity.class);
            intent.putExtra("hi","我来设置值");
            //启动RedActivity，为了从RedActivity带返回值使用startActivityForResult启动
            startActivityForResult(intent,988);
        }


    }

    //重写onActivityResult
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //启动Activity的Code值为988，回传Activitiy 的Code值为500
        if (requestCode==988 && resultCode==500){
            String resultValue = data.getStringExtra("result_key");

            Log.i("TAG",resultValue);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("TAG","MainActivity 的onStart已经执行~");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("TAG","MainActivity 的onResume已经执行~");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("TAG","MainActivity 的onPause已经执行~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("TAG","MainActivity 的onStop已经执行~");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("TAG","MainActivity 的onDestroy已经执行~");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("TAG","MainActivity 的onRestart已经执行~");
    }



}